package com.sqsf.controller;

import com.sqsf.entity.User;
import com.sqsf.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 显示用户列表
    @GetMapping("/user/list")
    public String userList(Model model) {
        model.addAttribute("users", userService.getAllUsers());
        return "userList";
    }

    // 跳转到添加用户页面
    @GetMapping("/user/add")
    public String toAddUser() {
        return "addUser";
    }

    // 处理添加用户表单提交
    @PostMapping("/user/add")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/user/list"; // 重定向到用户列表
    }
}